package com.yh.spring.aop;

import java.util.Date;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import com.yh.spring.mybatis.interceptor.LoginInterceptor;

@Component
@Aspect
public class LoggerHandler {
	private static Logger logger=LogManager.getLogger(LoginInterceptor.class);
	public static final String edp="exection(* com.yh.spring.mybatis.service.impl.*Impl.*(..))";
	@Before(edp)
	public void before(JoinPoint jp){
		System.out.println("**********前置通知**********");
		System.out.println(jp.getSignature().getName());
	}
	@After(edp)
	public void after(JoinPoint jp){
		System.out.println("**********最终通知**********");
	}
	@Around(edp)
	public Object logAround(ProceedingJoinPoint jp){
		long start = System.currentTimeMillis();

		String className = jp.getThis().toString();
		String methodName = jp.getSignature().getName();
		logger.debug("logAround开始:位于" + className + "调用" + methodName + "方法开始!现在时间是:" + new Date().toLocaleString()+"毫秒");
		System.out.println(
				"logAround开始:位于" + className + "调用" + methodName + "方法开始!现在时间是:" + new Date().toLocaleString()+"毫秒");
		Object[] args = jp.getArgs();
		Object obj = null;
		try {
			obj = jp.proceed(args);
		} catch (Throwable e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		logger.debug("logAround结束:位于" + className + "调用" + methodName + "方法结束!现在时间是:"
				+ new Date().toLocaleString() + "\n耗时:" + (end - start)+"毫秒");
		System.out.println("logAround结束:位于" + className + "调用" + methodName + "方法结束!现在时间是:"
				+ new Date().toLocaleString() + "\n耗时:" + (end - start)+"毫秒");

		return obj;
	}	}

